﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KD.Sms.UI_Sys
{
    public partial class frmLog : Form
    {
        public frmLog()
        {
            InitializeComponent();
        }

        private void frmLog_Load(object sender, EventArgs e)
        {
            DoBindLogTree();
        }


        private void DoBindLogTree()
        {
            if (System.IO.Directory.Exists(System.Environment.CurrentDirectory.ToString() + "\\log"))
            {
                treeCatalog.Nodes.Clear();
                treeCatalog.BeginUpdate();
                //treeCatalog.BeginUpdate();//首先禁止TreeViee的重绘
                DirectoryInfo root = new DirectoryInfo(System.Environment.CurrentDirectory.ToString() + "\\log");
                TreeNode NodeRoot = new TreeNode();
                NodeRoot.Text = "Log";
                NodeRoot.Name = string.Empty;
                NodeRoot.ImageIndex = 0;
                treeCatalog.Nodes.Add(NodeRoot);
                FileInfo[] _files = root.GetFiles();
                foreach (FileInfo fi in _files)
                {
                    TreeNode _ndFile = new TreeNode();
                    _ndFile.Text = fi.Name;
                    _ndFile.Name = fi.FullName;
                    _ndFile.ImageIndex = 1;
                    NodeRoot.Nodes.Add(_ndFile);
                }
                DirectoryInfo[] _dirs = root.GetDirectories();
                foreach (DirectoryInfo di in _dirs)
                {
                    TreeNode _ndDir = new TreeNode();
                    _ndDir.Text = di.Name;
                    _ndDir.Name = string.Empty;
                    _ndDir.ImageIndex = 0;
                    NodeRoot.Nodes.Add(_ndDir);

                    foreach (FileInfo fi in di.GetFiles())
                    {
                        TreeNode _ndFile = new TreeNode();
                        _ndFile.Text = fi.Name;
                        _ndFile.Name = fi.FullName;
                        _ndFile.ImageIndex = 1;
                        _ndDir.Nodes.Add(_ndFile);
                    }

                    if (di.GetDirectories().Length > 0)
                        DoBindDirectory(_ndDir, di);
                }
                treeCatalog.EndUpdate();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="Node"></param>
        /// <param name="diRoot"></param>
        private void DoBindDirectory(TreeNode Node, DirectoryInfo diRoot)
        {
            foreach (FileInfo fi in diRoot.GetFiles())
            {
                TreeNode _ndFile = new TreeNode();
                _ndFile.Text = fi.Name;
                _ndFile.Name = fi.FullName;
                _ndFile.ImageIndex = 1;
                Node.Nodes.Add(_ndFile);
            }
            DirectoryInfo[] _dirs = diRoot.GetDirectories();
            foreach (DirectoryInfo di in _dirs)
            {
                TreeNode _ndDir = new TreeNode();
                _ndDir.Text = di.Name;
                _ndDir.Name = string.Empty;
                _ndDir.ImageIndex = 0;
                Node.Nodes.Add(_ndDir);

                foreach (FileInfo fii in di.GetFiles())
                {
                    TreeNode _ndFile = new TreeNode();
                    _ndFile.Text = fii.Name;
                    _ndFile.Name = fii.FullName;
                    _ndFile.ImageIndex = 1;
                    _ndDir.Nodes.Add(_ndFile);
                }

                if (di.GetDirectories().Length > 0)
                {
                    DoBindDirectory(_ndDir, di);
                }
            }
        }



        private void treeCatalog_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node != null && !string.IsNullOrEmpty(e.Node.Name))
            {
                DoReadLog(e.Node.Name);
            }
        }

        private string strData;
        /// <summary>
        /// 获取文件中的数据
        /// </summary>
        /// <param name="args"></param>
        public void DoReadLog(String filePath)
        {
            try
            {
                using (StreamReader sr = new StreamReader(filePath, false))
                {
                    strData = sr.ReadToEnd().ToString();
                    sr.Close();
                    ricLog.Text = strData;
                }
            }
            catch { }
        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            DoBindLogTree();
        }

        private void mnuClear_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定清除当前日志信息？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ricLog.Clear();
                if (System.IO.Directory.Exists(System.Environment.CurrentDirectory.ToString() + "\\log"))
                    System.IO.Directory.Delete(System.Environment.CurrentDirectory.ToString() + "\\log", true);
                treeCatalog.Nodes.Clear();
                DoBindLogTree();
            }
        }
    }
}
